﻿// Jinx AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.CodeDom.Compiler;
using System.IO;

namespace Jinx.Models.Prototyping
{
    public class CompilationErrorBase : Exception
    {
        private readonly string compilerMessage;
        private readonly CompilerErrorCollection errors;

        protected CompilationErrorBase(string Message, CompilerErrorCollection Errors)
        {
            compilerMessage = Message;
            errors = Errors;
        }

        public string CompilerMessage
        {
            get { return compilerMessage; }
        }

        public CompilerErrorCollection Errors
        {
            get { return errors; }
        }
    }

    public class CompilationError<T> : CompilationErrorBase
    {
        public CompilationError(CompilerErrorCollection Errors) :
            base(string.Format("Could not compile prototype for type {0}: {1}",
                               typeof (T).Name, ErrorString(Errors)), Errors)
        {
        }

        private static string ErrorString(CompilerErrorCollection Errors)
        {
            using (var Writer = new StringWriter())
            {
                foreach (CompilerError Error in Errors)
                    Writer.WriteLine(Error.ToString());
                return Writer.ToString();
            }
        }
    }
}